<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('Ajax_model');
		$this->userinfo = $this->session->userdata('userinfo');
		$this->comment_key = '7qiaor!@#515';
	}
	function get_comments(){
		$data = array();
		$param = $this->input->post('param', true);
		$key = $this->input->post('key', true);
		if ($key == md5('lishuzulishuzu')) {
			if(intval($param['aid'])){
				$data['comments'] = $this->Ajax_model->get_comments(intval($param['aid']));
				$data['aid'] = $param['aid'];
				$data['key'] = md5($param['aid'].$this->comment_key);//debug			
			}
		}
		$data['userinfo'] = $this->userinfo;
		$this->load->template('comment_tpl', $data);
	}
	function add_comment(){
		$data = array();
		$param = $this->input->post('param', true);
		$content = $param['content'] ? $param['content'] : '';
		$aid = $param['aid'] ? intval($param['aid']) : '';
		$key = $this->input->post('key', true);
		if (!$this->userinfo['uid']) {
			showjsonmessage('您还没有登录,不能评论', '2');
		}
		if ($key == md5($param['aid'].$this->comment_key)) {
			if(intval($param['aid']) && !empty($content)){
				$comments = array(
						'aid' => $aid,
						'uid' => $this->userinfo['uid'],
						'ip' => getonlineip(),
						'content' => $content,
						'dateline' => now(),
					);
				$id = $this->Ajax_model->add_comment($comments);
				if ($id) {
					showjsonmessage('操作成功', '1');
				}else{
					showjsonmessage('操作失败', '3');
				}
			}
		}
		showjsonmessage('非法请求', '4');
	}
	
	function del_comment(){
		$data = array();
		$param = $this->input->post('param', true);
		$aid = $param['aid'] ? intval($param['aid']) : '';
		$cid = $param['cid'] ? intval($param['cid']) : '';
		$key = $this->input->post('key', true);
		if (!$this->userinfo['uid']) {
			showjsonmessage('您还没有登录,不能删除评论', '2');
		}
		if ($key == md5($aid.$this->comment_key)) {
			if(intval($aid) && intval($cid)){
				$res = $this->Ajax_model->del_comment($cid);
				if ($res == '1') {
					showjsonmessage('操作成功', '1');
				}elseif($res == '2'){
					showjsonmessage('不能删除他人评论', '2');
				}else{
					showjsonmessage('操作失败', '3');					
				}
			}
		}
		showjsonmessage('非法请求', '4');
	}
	
	function add_collection(){
		$data = array();
		$param = $this->input->post('param', true);
		$aid = $param['aid'] ? intval($param['aid']) : '';
		$key = $this->input->post('key', true);
		if (!$this->userinfo['uid']) {
			showjsonmessage('您还没有登录,不能收藏', '2');
		}
		if ($key == md5($param['aid'].'lishuzu7qiaor!@#')) {
			if(intval($param['aid'])){
				$collection = array(
						'aid' => $aid,
						'uid' => $this->userinfo['uid'],
						'dateline' => now(),
					);
				$result = $this->Ajax_model->add_collection($collection);
				if ($result['code'] == '1') {
					showjsonmessage('操作成功', '1');
				}elseif ($result['code'] == '2') {
					showjsonmessage('已经收藏过', '5');					
				}else{
					showjsonmessage('操作失败', '3');					
				}
			}
		}
		showjsonmessage('非法请求', '4');
	}

	function cancel_collection(){
		$data = array();
		$param = $this->input->post('param', true);
		$aid = $param['aid'] ? intval($param['aid']) : '';
		$key = $this->input->post('key', true);
		if (!$this->userinfo['uid']) {
			showjsonmessage('您还没有登录,不能取消收藏', '2');
		}
		if ($key == md5($param['aid'].'lishuzu7qiaor!@#')) {
			if(intval($param['aid'])){
				$result = $this->Ajax_model->del_collection($aid);
				if ($result['code'] == '1') {
					showjsonmessage('操作成功', '1');
				}elseif ($result['code'] == '2') {
					showjsonmessage('未登录', '2');					
				}else{
					showjsonmessage('操作失败', $result['code']);
				}
			}
		}
		showjsonmessage('非法请求', '4');
	}
}